﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace StyleMVVM.DependencyInjection.Conditions
{
	public sealed class WhenInjectedInto : IExportCondition
	{
		private Type[] injectedType;

		public WhenInjectedInto(params Type[] injectedType)
		{
			this.injectedType = injectedType;
		}

		public bool ConditionMeet(IDependencyInjectionContainer container,
		                          IInjectionContext injectionContext,
		                          IExportStrategy exportStrategy)
		{
			IInjectionTargetInfo target = injectionContext.Target;

			if (target != null)
			{
				Type injectionType = injectionContext.Target.InjectionType;

				if (injectionType != null)
				{
					TypeInfo injectionTypeInfo = injectionType.GetTypeInfo();

					foreach (Type type in injectedType)
					{
						if (type == null || type.GetTypeInfo().IsAssignableFrom(injectionTypeInfo))
						{
							return true;
						}
					}
				}
			}

			return false;
		}
	}
}